package com.pickgliss.loader
{
	import com.pickgliss.ui.ComponentSetting;
	import com.pickgliss.ui.core.Component;
	
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.net.URLLoaderDataFormat;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;
	import flash.utils.ByteArray;
	import flash.utils.getTimer;

	public class DisplayLoader extends BaseLoader
	{
		/**
		 * 决定是取本地文件还是取服务器文件
		 */		
		public static var isDebug:Boolean = false;
		public static var Context:LoaderContext;
		
		private static var _tempContext : LoaderContext;
		private static var _tempContainer : Sprite;
		
		public static function get tempContext(): LoaderContext
		{
			if(!_tempContext)
			{
				_tempContext = new LoaderContext(false,new ApplicationDomain(ApplicationDomain.currentDomain));
				_tempContainer = new Sprite();
				_tempContext.requestedContentParent = _tempContainer;
				_tempContext.allowCodeImport = true;
			}
				
			return _tempContext;
		}
		
		public function DisplayLoader(id:int,url:String)
		{
			initDisplayLoader();
			super(id,url,null);
		}
		protected function initDisplayLoader() : void
		{
			_displayLoader = new Loader();
		}
		
		protected var _displayLoader:Loader;
		
		override public function loadFromBytes(data:ByteArray):void
		{
			if(!_displayLoader || !data)return;
			data.position = 0;
			_displayLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,__onContentLoadComplete);
			_displayLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, __ioErrorHandler);
			if(type == BaseLoader.MODULE_LOADER || type == BaseLoader.RESOURCE_LOADER || type == BaseLoader.DOMAIN_RESOURCE_LOADER)
			{
				_displayLoader.loadBytes(data,Context);
			}else
			{
				_displayLoader.loadBytes(data,tempContext);
			}
		}
		
		private function __ioErrorHandler(evt : IOErrorEvent) : void
		{
			_displayLoader.contentLoaderInfo.removeEventListener(Event.COMPLETE,__onContentLoadComplete);
			_displayLoader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, __ioErrorHandler);
			fireCompleteEvent();
		}
		
		protected function __onContentLoadComplete(event:Event):void
		{
			trace("load useTime : "+String(getTimer() - _starTime));
			if(_displayLoader)
			{
				_displayLoader.contentLoaderInfo.removeEventListener(Event.COMPLETE,__onContentLoadComplete);
				_displayLoader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, __ioErrorHandler);
			}
			else
			{
				(event.currentTarget as LoaderInfo).removeEventListener(Event.COMPLETE,__onContentLoadComplete);
				(event.currentTarget as LoaderInfo).removeEventListener(IOErrorEvent.IO_ERROR, __ioErrorHandler);
			}
			fireCompleteEvent();
		}
		
		override protected function __onDataLoadComplete(event:Event):void
		{
			removeEvent();
			try{
				_loader.close();
			}
			catch(err :Error){}
			if(_loader.data.length == 0)return;
			var md5:String = ComponentSetting.getUrlMd5(_url);
			LoaderSavingManager.cacheFile(_url,_loader.data,false,md5);
			loadFromBytes(_loader.data);
		}
		
		override public function get content():*
		{
			if(_displayLoader.content is MovieClip)_displayLoader.content["biaoji"] = url;
			return _displayLoader.content;
		}
		
		override protected function getLoadDataFormat():String
		{
			return URLLoaderDataFormat.BINARY;
		}
		
		override public function get type():int
		{
			return BaseLoader.DISPLAY_LOADER;
		}
		override public function dispose():void
		{
			super.dispose();
			if(_tempContainer)
			{
				var count : int = _tempContainer.numChildren;
				for(var i :int=count-1;i>=0;i--)
				{
					_tempContainer.removeChildAt(i);
				}
			}
			if(_displayLoader){
				if(_displayLoader.contentLoaderInfo)
				{
					_displayLoader.contentLoaderInfo.removeEventListener(Event.COMPLETE,__onContentLoadComplete);
					_displayLoader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, __ioErrorHandler);
				}
				_displayLoader.unload();
				_displayLoader.unloadAndStop(true);
			}
			_displayLoader = null;
		}
	}
}